package com.javagroup.chessonline.server;

import java.io.BufferedReader;
import java.io.IOException;

import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import java.net.Socket;

import com.javagroup.chessonline.chessman.ChessData;
import com.javagroup.chessonline.chessman.Chessman;


public class Worker implements Runnable {
	int id;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	Server server;
	Socket clientSocket;
	public BufferedReader br = null;
	public PrintWriter pw = null;

	public ObjectOutputStream oos = null;
	public ObjectInputStream ois = null;

	public Worker(Socket client, Server server, int id) throws IOException {
		this.id = id;
		this.server = server;
		this.clientSocket = client;
		br = new BufferedReader(new InputStreamReader(
				clientSocket.getInputStream()));
		pw = new PrintWriter(new OutputStreamWriter(
				clientSocket.getOutputStream()));

		oos = new ObjectOutputStream(clientSocket.getOutputStream());
		ois = new ObjectInputStream(clientSocket.getInputStream());
		oos.flush();
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			clientLoop();
		} catch (Exception e) {
			e.printStackTrace();
		} /*
		 * finally { try { clientSocket.close(); } catch (IOException e) { //
		 * TODO Auto-generated catch block e.printStackTrace(); } }
		 */
	}

	/*
	 * private void clientLoop() throws IOException { while (true) { String
	 * line; if ((line = getStringData()) != null) { if (line.equals("TeamA")) {
	 * sendStringData("Your team is A"); } if (line.equals("TeamB")) {
	 * sendStringData("Your team is B"); }
	 * 
	 * 
	 * if (line.equals("Start now")) { // start game String currentTurn = "1";
	 * server.sendString("Current turn"); server.sendString(currentTurn);
	 * server.setCurrentTurn(Integer.parseInt(currentTurn)); } if
	 * (line.equals("Finish turn")) {
	 * 
	 * //Chessman[] temp = receiveArrayChessData(); ChessData data = (ChessData)
	 * receiveObject(); //server.sendString("Refresh board");
	 * server.sendChessData(data,id); int turn = server.getCurrentTurn();
	 * if(turn == 1){ turn = 0; } else{ turn = 1; } server.setCurrentTurn(turn);
	 * } if (line.equals("Get new turn")) { sendStringData("Current turn"); int
	 * turn = server.getCurrentTurn();
	 * 
	 * sendStringData(String.valueOf(turn));
	 * 
	 * } // System.out.println(line); } } }
	 */

	private void clientLoop() throws IOException {
		while (true) {
			ChessData data;
			if ((data = (ChessData) receiveObject()) != null) {
				if (data.getHeader().equals("TeamA")) {
					sendObject(new ChessData("Your team is A"));
					// sendStringData("Your team is A");
				}
				if (data.getHeader().equals("TeamB")) {
					sendObject(new ChessData("Your team is B"));
				}

				if (data.getHeader().equals("Start now")) {
					// start game
					//String currentTurn = "0";
					server.sendObject(new ChessData("Current turn",0));
					//server.sendString(currentTurn);
					server.setCurrentTurn(0);
				}
				if (data.getHeader().equals("Finish turn")) {

					 Chessman[] chess = data.getArrayChess();
				//	ChessData chess = (ChessData) receiveObject();
					// server.sendString("Refresh board");
					server.sendChessData(chess, id);
					int turn = server.getCurrentTurn();
					if (turn == 1) {
						turn = 0;
					} else {
						turn = 1;
					}
					server.setCurrentTurn(turn);
				}
				if (data.getHeader().equals("Get new turn")) {
					//sendStringData("Current turn");
					int turn = server.getCurrentTurn();
					server.sendObject(new ChessData("Current turn",turn));
					
				//	sendObject(new ChessData(String.valueOf(turn)));
					//sendStringData(String.valueOf(turn));

				}
				
			}
		}
	}

	public String getStringData() {
		String line = null;
		try {
			/*
			 * br = new BufferedReader(new InputStreamReader(
			 * clientSocket.getInputStream()));
			 */

			line = br.readLine();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return line;
	}

	public void sendStringData(String s) {

		// pw = new PrintWriter(new
		// OutputStreamWriter(clientSocket.getOutputStream()));

		pw.println(s);
		pw.flush();

	}

	public void sendArrayChessData(Chessman[] arrayChess) {
		Chessman[] obj = arrayChess;
		try {
			// oos = new ObjectOutputStream(clientSocket.getOutputStream());
			oos.writeObject(obj);
			oos.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public Chessman[] receiveArrayChessData() {
		Chessman[] obj = null;
		try {
			/*
			 * if(ois == null){
			 * 
			 * ois = new ObjectInputStream(clientSocket.getInputStream());
			 * 
			 * }
			 */
			obj = (Chessman[]) ois.readObject();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return obj;

	}

	public void sendObject(Object obj) {
		try {
			// oos = new ObjectOutputStream(clientSocket.getOutputStream());
			oos.writeObject(obj);
			oos.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public Object receiveObject() {
		Object obj = null;
		try {
			// ois = new ObjectInputStream(clientSocket.getInputStream());
			obj = ois.readObject();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return obj;
	}

}
